package com.xindacloud.sms.adapter.service.business.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xindacloud.sms.adapter.dao.SmsUserAllowOperatorMapper;
import com.xindacloud.sms.adapter.entity.SmsUserAllowOperator;
import com.xindacloud.sms.adapter.exception.ServiceException;
import com.xindacloud.sms.adapter.service.business.ISmsUserAllowOperatorService;
import com.xindacloud.sms.adapter.utils.UnionResultHelp;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;


@Slf4j
@Service
public class SmsUserAllowOperatorServiceImpl extends ServiceImpl<SmsUserAllowOperatorMapper, SmsUserAllowOperator> implements ISmsUserAllowOperatorService {


    @Override
    public List<SmsUserAllowOperator> queryUserAllowOperatorById(Integer userId) {
        return lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .list();
    }


    @Override
    public List<SmsUserAllowOperator> queryMaxPriceOrderByDesc(Integer userId) {

        List<SmsUserAllowOperator> minPricingList = lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getStatus, true)
                //.groupBy(SmsUserAllowOperator::getCountryCode,SmsUserAllowOperator::getPrefix,SmsUserAllowOperator::getNetworkCode)
                .orderByDesc(SmsUserAllowOperator::getPrice)
                .list();

        Optional.of(minPricingList)
                .filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ServiceException("该用户未同步运营商价格列表", UnionResultHelp.StatusEnum.ParamsFail.code()));
        return minPricingList;
    }


    @Override
    public List<SmsUserAllowOperator> queryMinCostPriceOrderByAsc(Integer userId) {

        List<SmsUserAllowOperator> minPricingList = lambdaQuery()
                .eq(SmsUserAllowOperator::getUserId, userId)
                .eq(SmsUserAllowOperator::getStatus, true)
                //.groupBy(SmsUserAllowOperator::getCountryCode,SmsUserAllowOperator::getPrefix,SmsUserAllowOperator::getNetworkCode)
                .orderByAsc(SmsUserAllowOperator::getCostPrice)
                .list();

        Optional.of(minPricingList)
                .filter(CollectionUtils::isNotEmpty)
                .orElseThrow(() -> new ServiceException("该用户未同步运营商价格列表", UnionResultHelp.StatusEnum.ParamsFail.code()));
        return minPricingList;
    }
}
